using System.Collections.Generic;
using LitJson;
using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelFAQ : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private List<CButton> cells = new List<CButton>();

        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelFAQ>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelFAQ>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelFAQ", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            Cell.gameObject.SetActive(false);
            ReqData();
        }

        private async void ReqData()
        {
            int mainApp = 1;
            var res = await Network.Get($"/v1/user/faq?category={mainApp}");
            JsonData listData = res.jsonData["data"]["faqs"];
            for (int i = 0; i < cells.Count; i++)
            {
                GameObject.Destroy(cells[i].gameObject);
            }
            cells.Clear();
            for (int i = 0; i < listData.Count; i++)
            {
                JsonData data = listData[i];
                CButton btn = GameObject.Instantiate(Cell, Cell.transform.parent, false);
                btn.gameObject.SetActive(true);
                btn.gameObject.Get<Text>("Title").text = StringHelper.SurportSpace(Utils.GetI18N(data["title"].ToString()));
                btn.gameObject.Get<Text>("Content").text = Utils.GetI18N(data["content"].ToString());
                btn.gameObject.Get<GameObject>("Content").SetActive(false);
                cells.Add(btn);
            }
            for (int i = 0; i < cells.Count; i++)
            {
                CButton btn = cells[i];
                int index = i;
                btn.SetClick(() =>
                {
                    if (btn.gameObject.Get<GameObject>("Content").activeSelf)
                    {
                        btn.gameObject.Get<GameObject>("Content").SetActive(false);
                        btn.gameObject.Get<RectTransform>("Jiantou").localEulerAngles = new Vector3(0, 0, 0);
                    }
                    else
                    {
                        for (int j = 0; j < cells.Count; j++)
                        {
                            cells[j].gameObject.Get<GameObject>("Content").SetActive(j == index);
                            cells[j].gameObject.Get<RectTransform>("Jiantou").localEulerAngles = new Vector3(0, 0, j == index ? 180 : 0);
                        }
                    }
                });
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
